var t, a = getApp();

Page({
    data: {
        items: [],
        startX: 0,
        startY: 0,
        toView: "blue",
        zujiIcon: "../../res/images/icon-zuji.png",
        zijiName: "推荐商品",
        selectedMenuId: 1,
        is_first_action: !0,
        total: {
            count: 0,
            money: 0
        },
        shopLists:[],
        allSelected:[],
        cart_list:[],
        shop_name:'',
        type: 1
    },
    // 批量删除
    allDelete:function(){
        let that = this;
        let ru_id = a.globalData.ru_id;
        let shopLists = that.data.shopLists[ru_id];
        console.log('批量删除',shopLists)
        for (let i = 0; i < shopLists.length; i++) {
            if (shopLists[i].is_checked == 1) {
                console.log(shopLists[i].rec_id)
            }
        }
    },
    // 状态切换
    change:function(e){
        let that = this;
        if (e.currentTarget.dataset.type == 1) {
            that.setData({
                type: 2
            })
        }else if (e.currentTarget.dataset.type == 2) {
            that.setData({
                type: 1
            })
        }
    },
    // 购物车商品复选框
    bindCheckbox: function(e) {
        var that = this;
		/*绑定点击事件，将checkbox样式改变为选中与非选中*/
        //拿到下标值，以在carts作遍历指示用
        var index = parseInt(e.currentTarget.dataset.index);
        //原始的icon状态
        for(var m in that.data.shopLists){
            var shopLists = that.data.shopLists;
            var s = e.currentTarget.dataset;
            var o = wx.getStorageSync("token");
            // 对勾选状态取反
            if(shopLists[m][index].is_checked==0){
                shopLists[m][index].is_checked = 1;
                wx.request({
                    url: a.apiUrl("cart/ToggleSelected"),
                    data: {
                        id: s.id,
                        is_checked: 1
                    },
                    header: {
                        "Content-Type": "application/json",
                        "X-ECTouch-Authorization": o
                    },
                    method: "POST",
                    success: function(res) {
                        that.onLoad();
                    }
                });
            }else if(shopLists[m][index].is_checked ==1){
                shopLists[m][index].is_checked = 0;
                wx.request({
                    url: a.apiUrl("cart/ToggleSelected"),
                    data: {
                        id: s.id,
                        is_checked: 0
                    },
                    header: {
                        "Content-Type": "application/json",
                        "X-ECTouch-Authorization": o
                    },
                    method: "POST",
                    success: function(res) {
                        that.onLoad();
                    }
                });
            }
            

            // 写回经点击修改后的数组
		    that.setData({
			    shopLists: shopLists
		    });
        }
    },
    // 全选
    allSelected:function(){
        var that = this;
        // console.log(that.data.allSelected)
        if(that.data.allSelected){
            for (var m in that.data.shopLists) {
                for (var n in that.data.shopLists[m]) {
                    that.data.shopLists[m][n].is_checked = 0
                }
            }
            that.setData({
                allSelected:false,
                shopLists:that.data.shopLists
            })
        }else{
            for (var m in that.data.shopLists) {
                for (var n in that.data.shopLists[m]) {
                    that.data.shopLists[m][n].is_checked = 1
                }
            }
            that.setData({
                allSelected:true,
                shopLists:that.data.shopLists
            })
        }
        var o = wx.getStorageSync("token");
        if(that.data.allSelected){
            wx.request({
                url: a.apiUrl("cart/ToggleAllSelected"),
                header: {
                    "Content-Type": "application/json",
                    "X-ECTouch-Authorization": o
                },
                method: "POST",
                data:{
                    is_checked: 1,
                    ids:that.data.cart_list
                },
                success:function(res){
                    that.onLoad();
                }
            })
        }else{
            wx.request({
                url: a.apiUrl("cart/ToggleAllSelected"),
                header: {
                    "Content-Type": "application/json",
                    "X-ECTouch-Authorization": o
                },
                method: "POST",
                data:{
                    is_checked: 0,
                    ids:that.data.cart_list
                },
                success:function(res){
                    that.onLoad();
                }
            })
        }
    },
    getCartGoods: function(t) {
        var that = this;
        var o = wx.getStorageSync("token");
        wx.request({
            url: a.apiUrl("cart"),
            method: "POST",
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": o
            },
            success: function(a) {
                var o, e = "";
                // console.log('getCartGoods-购物车商品',a.data.data.cart_list);
                that.setData({
                    cart_list:[]
                })
                for (var m in a.data.data.cart_list) {
                    for (var n in a.data.data.cart_list[m]) {
                        that.data.cart_list.push(a.data.data.cart_list[m][n].rec_id);
                        // 去重
                        var _cart_list = [];
                        for (var i = 0; i < that.data.cart_list.length; i++) {
                            if (!_cart_list.includes(that.data.cart_list[i])) {
                                _cart_list.push(that.data.cart_list[i])
                            }
                        }
                        // 重新赋值
                        that.setData({
                            cart_list:_cart_list,
                            shop_name: a.data.data.cart_list[m][n].shop_name
                        })
                    }
                }
                // **********************
                if(a.data.data.total.selected_number == a.data.data.total.real_goods_count){
                    that.setData({
                        allSelected : true
                    })
                }else{
                    that.setData({
                        allSelected : false
                    })
                }
                // **********************

                for (var n in a.data.cart_list) {
                    o = a.data.data.cart_list[n].goods_attr.split("\n"), e = "";
                    for (var i in o) "" != o[i] && (e += o[i] + ",");
                    a.data.cart_list[n].goods_attr = e.substring(0, e.length - 1);
                }
                "" == a.data.data.cart_list || void 0 == a.data.data.cart_list ? t.setData({
                    shopLists: "",
                    // indexGoods: a.data.data.best_goods, //底部可能你想要
                    cartData: a.data.data
                }) : t.setData({
                    shopLists: a.data.data.cart_list,
                    total: a.data.data.total,
                    // indexGoods: a.data.data.best_goods, //底部可能你想要
                    cartData: a.data.data
                });
            }
        });
    },
    onLoad: function() {
        t = wx.getStorageSync("token");
        var a = this;
        this.getCartGoods(a), 
        this.loadingChange();
    },
    onShow: function() {
        // a.getUserInfo();
        var t = this;
        this.getCartGoods(t), 
        this.loadingChange(), 
        t.setData({
            is_first_action: !0
        });

      // 改的 底部可能你想要【店铺】
      var i = a.apiUrl("store/detail2");
      var o = wx.getStorageSync("token");
      wx.request({
        url: i,
        data: {
          id: a.globalData.ru_id,
          ru_id:a.globalData.ru_id,
          page: 1,
          per_page: 10,
          cate_key: "is_on_sale",
          sort: "sort_order",
          order: "ASC",
          cat_id: 53
        },
        header: {
          "Content-Type": "application/json",
          "X-ECTouch-Authorization": o
        },
        method: "POST",
        success: function (res) {
          t.setData({
            indexGoods: res.data.goods,
          })
        }
      });
    },
    loadingChange: function() {
        var t = this;
        setTimeout(function() {
            t.setData({
                hidden: !0
            });
        }, 1e3);
    },
    addCount: function(o) {
        var e, n, i = this, 
        s = o.currentTarget.dataset, 
        r = i.data.total, 
        d = i.data.shopLists, 
        c = [];
        for (var u in d) c.push(d[u]);
        for (n in c) if (void 0 != (e = c[n].find(function(t) {
            return t.rec_id == s.id;
        }))) break;
        e.goods_number = parseInt(e.goods_number) + 1, 
        r.count += 1, 
        d.total_price += parseInt(e.goods_price), 
        wx.request({
            url: a.apiUrl("cart/update"),
            data: {
                id: s.id,
                amount: e.goods_number
            },
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": t
            },
            method: "POST",
            success: function() {
                i.onLoad();
            }
        });
    },
    minusCount: function(o) {
        var e, n, i = this, s = o.currentTarget.dataset, r = (this.data.total, this.data.shopLists), d = [];
        for (var c in r) d.push(r[c]);
        for (n in d) if (void 0 != (e = d[n].find(function(t) {
            return t.rec_id == s.id;
        }))) break;
        if (r.total_price -= parseInt(e.goods_price), parseInt(r.total_price) < 0) r.total_price += parseInt(r.goods_price); else {
            if (e.goods_number = parseInt(e.goods_number) - 1, parseInt(e.goods_number) < 1) return e.goods_number = parseInt(e.goods_number) + 1, 
            void (r.total_price += parseInt(e.goods_price));
            wx.request({
                url: a.apiUrl("cart/update"),
                data: {
                    id: s.id,
                    amount: e.goods_number
                },
                header: {
                    "Content-Type": "application/json",
                    "X-ECTouch-Authorization": t
                },
                method: "POST",
                success: function() {
                    i.onLoad();
                }
            });
        }
    },
    del: function(o) {
        var e = this, n = o.currentTarget.dataset;
        wx.showModal({
            title: "提示",
            content: "您确定要移除当前商品吗?",
            success: function(o) {
                o.confirm && wx.request({
                    url: a.apiUrl("cart/delete"),
                    data: {
                        id: n.id
                    },
                    method: "POST",
                    header: {
                        "Content-Type": "application/json",
                        "X-ECTouch-Authorization": t
                    },
                    success: function(t) {
                        e.onShow();
                    }
                });
            }
        });
    },
    flowcartBtn: function() {
        wx.switchTab({
            url: "../index/index"
        });
    },
    flowCheckoutBtn: function(t) {
        var o = this;
        var e = wx.getStorageSync("token");
        var shop_name = o.data.shop_name;
        wx.setStorageSync("flowcheckout", {
            from: "checkout"
        }), 
        1 == o.data.is_first_action && (o.setData({
            is_first_action: !1
        }), 
        wx.request({
            url: a.apiUrl("user/address/list"),
            method: "POST",
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": e
            },
            success: function(t) {
                "" != t.data.data ? wx.navigateTo({
                  url: "../flow/checkout?page_from=settlement&shop_name="+shop_name
                }) : wx.navigateTo({
                    url: "../address/create"
                });
            }
        }));
    }
});